#!/usr/bin/env python
# encoding: utf-8


"""
@time: 2017/3/29 下午6:13
"""
from mathsolver.functions.base import *
from sympy import expand_trig, pi, trigsimp, simplify
from mathsolver.functions.sanjiao import sanjiao_utils as su
from mathsolver.functions.budengshi import common_opers as co
from mathsolver.functions.sanjiao.huajian import trig_simplify
from mathsolver.functions.sanjiao.xiangxian import XiangXian
from mathsolver.functions.budengshi.budengshi_consts import BuDengShiZuSearchKey
from mathsolver.functions.sanjiao.conditions import SanJiaoTuxiangZhouQiYiDong
from mathsolver.functions.sanjiao.dandiaoxing import SanJiaoDanDiaoQuJian
from mathsolver.functions.sanjiao.tuxiang import SanJiaoHanShuTuXiangBianHuan
from mathsolver.functions.sanjiao.sanjiaobudengshi import JieSanJiaoBuDengShi
from mathsolver.functions.sanjiao.zhouqi import ZhouQi
from mathsolver.functions.sanjiao.sanjiao_constants import SanJiaoHanShuJiOuXingType
from mathsolver.functions.sanjiao.jiouxing import SanJiaoHanShuJiOuXingPanDuan


# 选择题
# ------------------------判断角象限 Begin-------------------------
# 若sinα<0,tanα>0,则α是() ["第一象限角", "第二象限角", "第三象限角", "第四象限角"]
class PanDuanXiangXian001(BaseFunction):
    def solver(self, *args):
        self.label.add('判断角象限')
        text = args[1]
        ineqs = BaseIneqs(self.search(BuDengShiZuSearchKey))
        symb_quads = list(XiangXian().solver(ineqs).output[0].sympify().values()[0])
        select_quad = None
        if text.find('第一象限') >= 0:
            select_quad = 1
        elif text.find('第二象限') >= 0:
            select_quad = 2
        elif text.find('第三象限') >= 0:
            select_quad = 3
        elif text.find('第四象限') >= 0:
            select_quad = 4
        if select_quad in symb_quads:
            self.output.append(BaseVariable(True))
        else:
            self.output.append(BaseVariable(False))
        return self


# ------------------------判断角象限 End-------------------------


# ------------------------图像移动的 Begin-------------------------
# 062 将函数y=2sin(2x+\\frac{π}{6})的图象向右平移\\frac{1}{4}个周期后,所得图象对应的函数为()
# ["y=2sin(2x+\\frac{π}{4})", "y=2sin(2x+\\frac{π}{3})", "y=2sin(2x-\\frac{π}{4})", "y=2sin(2x-\\frac{π}{3})"]
class TuXiangZhouQiYiDongXuanZhe(BaseFunction):
    def solver(self, *args):
        src_sinf = args[0]
        text = args[1]
        unit = args[2]
        _, solve_f = SanJiaoTuxiangZhouQiYiDong().solver(src_sinf, text, unit).output[0].sympify()
        _, option_f = args[3].sympify()
        option_f = trigsimp(expand_trig(option_f))
        if simplify(solve_f / option_f) == 1:
            self.output.append(BaseVariable(True))
        else:
            self.output.append(BaseVariable(False))
        return self


# 为了得到函数 y=\\sin (2x-\\frac{π }{6})的图象,可以将函数y=\\cos 2x的图象(  )
# "向右平移\\frac{\\pi }{6}个单位长度","向右平移\\frac{\\pi }{3}个单位长度","向左平移\\frac{\\pi }{6}个单位长度","向左平移\\frac{\\pi }{3}个单位长度"
class TuXiangYiDongXuanZhe(BaseFunction):
    def solver(self, *args):
        src_f = args[0]
        move_text = args[1]
        move_unit = args[2]
        _, target_f = args[3].sympify()
        solve_f = SanJiaoHanShuTuXiangBianHuan().solver(src_f, move_text, move_unit).output[0].sympify()
        target_f = trigsimp(expand_trig(target_f))
        flag = solve_f / target_f == 1
        self.output.append(BaseVariable(flag))
        return self


# ------------------------图像移动的 End-------------------------


# ------------------------单调性 Begin----------------------------
# 064 函数y=2cos^{2}x的一个单调增区间是().
# "(-\\frac{π}{4},\\frac{π}{4})", "(0,\\frac{π}{2})", "(\\frac{π}{4},\\frac{3π}{4})", "(\\frac{π}{2},π)"
class DanDiaoQuJianXuanZe(BaseFunction):
    @staticmethod
    def _has_interval(param_intl, intl):
        """
        给定一个具体的区间 和一个 区间集合 判断这个具体的区间有没有可能落在这个 区间集合上
        :param param_intl: 
        :param intl: 
        :return: 
        """
        left = param_intl.left
        left_open = param_intl.left_open
        right = param_intl.right
        right_open = param_intl.right_open
        k_symbol = list(left.free_symbols)[0]
        k_range = range(-10, 10)
        for i in k_range:
            left_value = left.subs(k_symbol, i)
            right_value = right.subs(k_symbol, i)
            tmp_intl = Interval(left_value, right_value, left_open, right_open)
            if intl.is_subset(tmp_intl):
                return True
        return False

    def solver(self, *args):
        f = args[0]
        text = args[1]
        option_intl = args[2].interval
        solve_f = SanJiaoDanDiaoQuJian(verbose=True).solver(f, text)
        self.steps.extend(solve_f.steps)
        solve_intl = solve_f.output[0].sympify().values()[0]
        if su.maybe_in_interval(solve_intl, option_intl):  # DanDiaoQuJianXuanZe._has_interval(solve_intl, option_intl)
            self.output.append(BaseVariable(True))
        else:
            self.output.append(BaseVariable(False))
        return self


# 函数过点 通过解三角函数回得出变量的取值列表
class SanJiaoHanShuGuoDianXuanZe001(BaseFunction):
    def solver(self, *args):
        x_symbol = args[0].sympify()  # x变量
        option_value = args[1].sympify()  # 选项
        x_values = self.search(x_symbol)
        flag = False
        for x_v in x_values:
            x_v = co.mm_expr_sub(x_v)
            k_value = co.isolve_eq2(BaseEq([x_v, option_value]))[0]
            if k_value in S.Integers:
                flag = True
                break
        self.output.append(BaseVariable(flag))
        return self


# ------------------------函数过点问题 End----------------------------

# ------------------------根据已知的不等式 判断其他不等式的正确性  Begin ----------------------------
# ""若tanα>0,则()"", ""options"": [""sinα>0"", ""cosα>0"", ""sin2α>0"", ""cos2α>0""]
class SanJiaoHanBuDengShiXuanZe001(BaseFunction):
    @staticmethod
    def _two_interval_eqed(intl1, intl2):
        return intl1.left == intl2.left and intl1.right == intl2.right and \
               intl1.left_open == intl2.left_open and intl1.right_open == intl2.right_open

    def solver(self, *args):
        cond_ineq = args[0]
        l1, op1, r1 = cond_ineq.sympify()
        l1 = expand_trig(l1)
        r1 = expand_trig(r1)
        option_ineq = args[0]
        l2, op2, r2 = option_ineq.sympify()
        l2 = expand_trig(l2)
        r2 = expand_trig(r2)
        cond_ineq = BaseIneq([l1, op1, r1])
        option_ineq = BaseIneq([l2, op2, r2])
        cond_solve = JieSanJiaoBuDengShi().solver(cond_ineq).output[0].sympify().values()[0]
        option_solve = JieSanJiaoBuDengShi().solver(option_ineq).output[0].sympify().values()[0]
        flag = False
        if type(cond_solve) == type(option_solve):
            if type(cond_solve) == Interval:
                flag = SanJiaoHanBuDengShiXuanZe001._two_interval_eqed(cond_solve, option_solve)
            elif type(cond_solve) == Union:
                cond_solve1, cond_solve2 = cond_solve.args
                option_solve1, option_solve2 = cond_solve.args
                flag = (SanJiaoHanBuDengShiXuanZe001._two_interval_eqed(cond_solve1, option_solve1) and
                        SanJiaoHanBuDengShiXuanZe001._two_interval_eqed(cond_solve2, option_solve2))
        self.output.append(BaseVariable(flag))
        return self


# ------------------------根据三角函数的值 判断其他三角函数的值  End ----------------------------


# 066 y=(sinx-cosx)^{2}-1是() "最小正周期为2π的偶函数", "最小正周期为2π的奇函数", "最小正周期为π的偶函数", "最小正周期为π的奇函数"
class ZuiXiaoZhengZhouQiXuanZe001(BaseFunction):
    def solver(self, *args):
        y_symbol, trig_f = args[0].sympify()
        cond_cycle = args[1].sympify()
        trig_cycle = ZhouQi(verbose=True).solver(trig_f).output[0].sympify()
        flag = cond_cycle == trig_cycle
        self.output.append(BaseVariable(flag))
        return self


# 三角函数奇偶性判断
class SanJiaoHanShuJiOuXingXuanZe001(BaseFunction):
    def solver(self, *args):
        y_symbol, trig_f = args[0].sympify()
        trig_f = trig_simplify(trig_f)
        option_text = args[1]
        func_type = SanJiaoHanShuJiOuXingType.ODD_FUNC if option_text.find(
            '奇') >= 0 else SanJiaoHanShuJiOuXingType.EVEN_FUNC
        solve_f = SanJiaoHanShuJiOuXingPanDuan().solver(BaseEq([y_symbol, trig_f]))
        self.steps.extend(solve_f.steps)
        trig_func_type = list(solve_f.output[0].sympify().values()[0])[0]
        flag = str(trig_func_type) == str(func_type)
        self.output.append(BaseVariable(flag))
        return self


# ------三角函数最小正周期选择---------
# ----函数y=\\sin ^{4}x + \\cos ^{2}x的最小正周期为------
# 在函数y=cos|2x|,y=|cosx|,y=cos(2x+\\frac{π}{6})y=tan(2x-\\frac{π}{4})中,最小正周期为π的所有函数为() options
#  [""①②③"", ""①③④"", ""②④"", ""①③""], ""subquestions"": []}"
class SanJiaoHanShuZhouQiXuanZe(BaseFunction):
    def solver(self, *args):
        return self


# 角的关系选择题
# style1 Input paramer1: eq(角的关系); paramer2: 关系式(等式)
class JiaoDeGuanXiXuanZeTi(BaseFunction):
    @staticmethod
    def get_par_value(intl):
        _vs = [pi / 6, pi / 4, pi / 3, pi / 2, 3 * pi / 2, pi]
        return list(filter(lambda _: _ in intl, _vs))[0]

    def solver(self, *args):
        self.label.add('判断角的关系-三角恒等变换')
        arg1, arg2 = args
        flag = False
        symbs = map(sympify, co.eqs_symbs([arg1.sympify()]))
        s1, s2 = symbs
        s1_intl = self.search(s1)
        s2_intl = self.search(s2)
        for s1_v in [pi / 6, pi / 4, pi / 3, pi / 2, 3 * pi / 2, pi]:
            if s1_v in s1_intl:
                l, r = arg1.sympify()
                f = l - r
                f = f.subs(s1, s1_v)
                s2_expr = su.solve_trig_eq(BaseEq([f, '0']))[0]
                s2_vs = su.expr_value_list(s2_expr, s2_intl)
                if s2_vs:
                    s2_v = s2_vs[0]
                    l1, r1 = arg2.sympify()
                    f1 = l1 - r1
                    if f1.subs(s1, s1_v).subs(s2, s2_v) == 0:
                        flag = True
        self.output.append(BaseVariable(flag))
        return self


if __name__ == '__main__':
    pass
